import { Nodes } from '@knno/dom';
import { Tag as UITag, TagEventMap, TagValue, TagPopupHandler } from '../components/tag/tag';
import { JSXProps } from '@knno/dom/jsx';
import { AttrCheck, isArray, isBool, isFunc, isNumber, isStr, jsxBuild, On } from './utils';

export { UITag, TagValue };

interface TagProps extends JSXProps<UITag>, On<TagEventMap, UITag> {
	disabled?: boolean;
	readonly?: boolean;
	selectOnly?: boolean;
	prefixIcon?: string;
	placeholder?: string;
	max?: number;
	maxLength?: number;
	text?: string;
	value?: TagValue[];
	invalid?: boolean;
	options?: TagValue[];
	onPopup?: TagPopupHandler<UITag>;
}

const ATTRS: AttrCheck = {
	disabled: isBool,
	readonly: isBool,
	selectOnly: isBool,
	prefixIcon: isStr,
	placeholder: isStr,
	max: isNumber,
	maxLength: isNumber,
	text: isStr,
	value: isArray,
	invalid: isBool,
	options: isArray,
	onPopup: isFunc,
};

export function Tag(props: TagProps, children: Nodes[]): UITag {
	return jsxBuild(new UITag(), props, children, ATTRS);
}
